package com.langfeiyes.pattern.memento.demo2;


import java.util.Date;
import java.util.UUID;

/**
 * Git备忘录原发器
 */
public class GitOriginator {
    //模拟保存版本内容
    private String content;

    //创建备忘录
    public IVersonCtrlMemento commit(){
        System.out.println("提交版本内容：" + this.content);
        return new GitMemento(this.content);
    }

    //恢复Originator 原发器状态
    public void reset(IVersonCtrlMemento memento){
        System.out.println("恢复版本，版本id："+ memento.getCommentId());
        GitMemento mi = (GitMemento) memento;
        this.content = mi.content;
        System.out.println("恢复版本，版本内容："+ this.content);
    }
    //Git备忘录
    private  class  GitMemento implements IVersonCtrlMemento {
        private String content;
        private Date date;  //时间
        private String commitId;  //使用uuid当做commit id

        @Override
        public String getCommentId() {
            return this.commitId;
        }

        @Override
        public void info() {
            System.out.println("当前版本时间：" + this.date);
            System.out.println("当前版本号：" + this.commitId);
            System.out.println("当前Git版本内容：" + content);
            System.out.println("---------------------------------");
        }

        public GitMemento(String content){
            this.content = content;
            this.date = new Date();
            this.commitId = UUID.randomUUID().toString();
        }
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void info(){
        System.out.println("当前版本内容：" + this.content);
    }
}
